package com.info.zhiduoduo.pay.service;

import com.alibaba.fastjson.JSONObject;
import com.info.zhiduoduo.common.configs.ZhaoShangPayConfig;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.CreateSubLedgerFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.DeleteSubLedgerFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.QuerySubLedgerBalanceFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.QuerySubLedgerHistoryTradeFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.QuerySubLedgerStatusFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.QuerySubLedgerTradeFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangtradebulter.SubLedgerPayFormBean;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.common.HeadReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.common.PatternInfo;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.common.RequestDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.common.ResponseDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.CreateSubLedgerReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.DeleteSubLedgerReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.QuerySubLedgerHistoryTradeReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.QuerySubLedgerStatusReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.QuerySubLedgerTradeReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.SubLedgerPayReqDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.request.tradebulter.detail.*;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.response.tradebulter.QuerySubLedgerBalanceRespDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.response.tradebulter.QuerySubLedgerHistoryTradeRespDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.response.tradebulter.QuerySubLedgerStatusRespDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.response.tradebulter.QuerySubLedgerTradeRespDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.model.response.tradebulter.SubLedgerPayRespDTO;
import com.info.zhiduoduo.pay.sdk.zhaoshangbank.service.ZhaoshangTradeButlerApiService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

/**
 * 招商银行交易管家
 */
@Slf4j
@Service
public class ZhaoShangTradeBulterService {

	@Resource
	ZhaoshangTradeButlerApiService zhaoshangTradeButlerApiService;

	@Resource
	ZhaoShangPayConfig zhaoShangPayConfig;

	/**
	 * 5.5.2.1新增记账子单元 NTDMAADD
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<?>> createSubLedger(CreateSubLedgerFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getAddSubLedger(), zhaoShangPayConfig.getUserId());
		//构建body中子帐本信息
		List<CreateSubLedgerInfo> info = CreateSubLedgerInfo.ofList(formBean);
		//构建body
		CreateSubLedgerReqDTO reqDTO = new CreateSubLedgerReqDTO().setNtdmaaddx(info);

		ResponseDTO<?> responseDTO = zhaoshangTradeButlerApiService.createSubLedger(RequestDTO.of(headReqDTO, reqDTO));
		return SmartResult.success(responseDTO);
	}

	/**
	 * 5.5.2.4 关闭记账子单元NTDMADLT
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult deleteSubLedger(DeleteSubLedgerFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getDeleteSubLedger(), zhaoShangPayConfig.getUserId());
		//构建body中模式信息
		List<DeleteSubLedgerAccountNoInfo> accountNoInfos = DeleteSubLedgerAccountNoInfo.ofList(formBean);
		//构建body中子帐本信息
		List<DeleteSubLedgerNoInfo> ledgerNoInfos = DeleteSubLedgerNoInfo.ofList(formBean);
		//构建body
		DeleteSubLedgerReqDTO reqDTO = new DeleteSubLedgerReqDTO().setNtdmadltx1(accountNoInfos).setNtdmadltx2(ledgerNoInfos);

		ResponseDTO<?> responseDTO = zhaoshangTradeButlerApiService.deleteSubLedger(RequestDTO.of(headReqDTO, reqDTO));
		return SmartResult.success(responseDTO);
	}

	/**
	 * 修改记账子单元NTDMAMNT
	 *
	 * @param info
	 * @return
	 */
	public SmartResult updateSubLedger(UpdateSubLedgerInfo info) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getUpdateSubLedger(), zhaoShangPayConfig.getUserId());
		//构建body中模式信息
		List<UpdateSubLedgerInfo> accountNoInfos = UpdateSubLedgerInfo.ofList(info);
		//构建body
		JSONObject reqDTO = new JSONObject();
		reqDTO.put("ntdmamntx1",accountNoInfos);

		ResponseDTO<?> responseDTO = zhaoshangTradeButlerApiService.updateSubLedger(RequestDTO.of(headReqDTO, reqDTO));
		return SmartResult.success(responseDTO);
	}

	/**
	 * 5.5.2.15查询记账子单元余额NTDMABAL
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<QuerySubLedgerBalanceRespDTO>> querySubLedgerBalance(QuerySubLedgerBalanceFormBean formBean) {
//		//构建head信息
//		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getQuerySubLedgerBalance(), zhaoShangPayConfig.getUserId());
//		//构建body中子帐本信息
//		List<QuerySubLedgerBalanceInfo> querySubLedgerBalanceInfos = QuerySubLedgerBalanceInfo.ofList(formBean);
//		//构建body
//		QuerySubLedgerBalanceReqDTO reqDTO = new QuerySubLedgerBalanceReqDTO().setNtdmabalx(querySubLedgerBalanceInfos);

		ResponseDTO<QuerySubLedgerBalanceRespDTO> responseDTO = zhaoshangTradeButlerApiService.querySubLedgerBalance(formBean);
		return SmartResult.success(responseDTO);
	}

	/**
	 * 5.5.2.3 查询记账子单元信息NTDMALST
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<QuerySubLedgerStatusRespDTO>> querySubLedgerStatus(QuerySubLedgerStatusFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getQueryLedgerStatus(), zhaoShangPayConfig.getUserId());
		//构建body中子帐本信息
		List<QuerySubLedgerStatusInfo> querySubLedgerBalanceInfos = QuerySubLedgerStatusInfo.ofList(formBean);
		//构建body
		QuerySubLedgerStatusReqDTO reqDTO = new QuerySubLedgerStatusReqDTO().setNtdmalstx(querySubLedgerBalanceInfos);

		ResponseDTO<QuerySubLedgerStatusRespDTO> responseDTO = zhaoshangTradeButlerApiService.querySubLedgerStatus(
				RequestDTO.of(headReqDTO, reqDTO));
		return SmartResult.success(responseDTO);
	}

	/**
	 * 5.5.2.21记账子单元内部转账NTDMATRX
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<SubLedgerPayRespDTO>> subLedgerPay(SubLedgerPayFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getLedgerPay(), zhaoShangPayConfig.getUserId());
		//构建body中明细信息
		List<SubLedgerPayInfo> infos = SubLedgerPayInfo.ofList(formBean);
		//构建body
		SubLedgerPayReqDTO reqDTO = new SubLedgerPayReqDTO().setNtdmatrxx1(infos);
		ResponseDTO<SubLedgerPayRespDTO> responseDTO = zhaoshangTradeButlerApiService.subLedgerPay(RequestDTO.of(headReqDTO, reqDTO));

		return SmartResult.success(responseDTO);
	}

/*	*//**
	 * 5.5.1.2.8记账子单元退款经办NTOPRDMR
	 *
	 * @param formBean
	 * @return
	 *//*
	public SmartResult<ResponseDTO<SubLedgerRefundRespDTO>> subLedgerRefund(SubLedgerRefundFormBean formBean) {
		PaymentMerchant paymentMerchant = new PaymentMerchant().selectById(formBean.getPaymentMerchant());
		log.info("获取到的支付商户:{}", JSON.toJSONString(paymentMerchant));
		Asserts.notNull(paymentMerchant, StatusMsg.StatusMsg_459);
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getLedgerRefund(), paymentMerchant.getMerchantNo());
		//构建body中模式信息
		List<PatternInfo> patternInfo = PatternInfo.ofLost(zhaoShangPayConfig.getTradeBulterModeCode());
		//构建body中明细信息
		List<SubLedgerRefundInfo> infos = SubLedgerRefundInfo.ofList(formBean, paymentMerchant);
		List<SubLedgerRefundAddress> addr = SubLedgerRefundAddress.ofList(formBean.getRpyadr());
		//构建body
		SubLedgerRefundReqDTO reqDTO = new SubLedgerRefundReqDTO().setNtbusmody(patternInfo).setNtoprdmrx1(infos).setNtoprdmrx2(addr);
		ResponseDTO<SubLedgerRefundRespDTO> responseDTO = zhaoshangTradeButlerApiService.subLedgerRefund(RequestDTO.of(headReqDTO,
				reqDTO));

		return SmartResult.success(responseDTO);
	}*/

	/**
	 * 5.5.2.13查询记账子单元当天交易NTDMTLST
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<QuerySubLedgerTradeRespDTO>> querySubLedgerTodayTrade(QuerySubLedgerTradeFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getQueryLedgerTodayTrade(), zhaoShangPayConfig.getUserId());
		//构建body中汇总信息
		List<QuerySubLedgerTradeInfo> infos = QuerySubLedgerTradeInfo.ofList(formBean);
		//构建body
		QuerySubLedgerTradeReqDTO reqDTO = new QuerySubLedgerTradeReqDTO().setNtdmtlsty(infos);
		ResponseDTO<QuerySubLedgerTradeRespDTO> responseDTO = zhaoshangTradeButlerApiService.querySubLedgerTrade(
				RequestDTO.of(headReqDTO, reqDTO));

		return SmartResult.success(responseDTO);
	}

	/**
	 * 5.5.2.14查询记账子单元历史交易NTDMTHLS
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult<ResponseDTO<QuerySubLedgerHistoryTradeRespDTO>> querySubLedgerHistoryTrade(QuerySubLedgerHistoryTradeFormBean formBean) {
		//构建head信息
		HeadReqDTO headReqDTO = HeadReqDTO.of(zhaoShangPayConfig.getQueryLedgerHistoryTrade(), zhaoShangPayConfig.getUserId());
		//构建body中汇总信息
		List<QuerySubLedgerHistoryTradeInfo> infos = QuerySubLedgerHistoryTradeInfo.ofList(formBean);
		//构建body
		QuerySubLedgerHistoryTradeReqDTO reqDTO = new QuerySubLedgerHistoryTradeReqDTO().setNtdmthlsy(infos);
		ResponseDTO<QuerySubLedgerHistoryTradeRespDTO> responseDTO = zhaoshangTradeButlerApiService.querySubLedgerHistoryTrade(
				RequestDTO.of(headReqDTO, reqDTO));

		return SmartResult.success(responseDTO);
	}

}
